package com.console.front.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.console.front.model.MovsModel;
import com.console.front.model.UserModel;
import com.console.front.service.UserService;

@Controller
@RequestMapping
public class IndexContorller {
	
	@Autowired
	private UserService userService;
	
	private static final String RESULT = "result";
	private static final String OK = "ok";
	private static final String NO = "no";
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public ModelAndView login(String username,String password){
		ModelAndView view = new ModelAndView("/json");
		UserModel model = userService.login(username, password);
		JSONObject jObj = new JSONObject();
		if(model != null){
			jObj.put(RESULT, OK);
			jObj.put("user", model);
		}else{
			jObj.put(RESULT, NO);
		}
		view.addObject("models", jObj);
		return view;
	}
	
	@RequestMapping(value="/reg", method=RequestMethod.POST)
	public ModelAndView reg(String username,String password){
		ModelAndView view = new ModelAndView("/json");
		UserModel model = userService.reg(username, password);
		JSONObject jObj = new JSONObject();
		if(model != null){
			jObj.put(RESULT, OK);
			jObj.put("user", model);
		}else{
			jObj.put(RESULT, NO);
		}
		view.addObject("models", jObj);
		return view;
	}
	
	@RequestMapping(value="/initMovs", method=RequestMethod.POST)
	public ModelAndView initMovs(){
		ModelAndView view = new ModelAndView("/view");
		userService.initMovs("http://www.3movs.com/");
		return view;
	}
	
	@RequestMapping(value="/findMovs", method=RequestMethod.POST)
	public ModelAndView findMovs(String pageNum,String pageSize){
		ModelAndView view = new ModelAndView("/json");
		List<MovsModel> models =  userService.findMovs(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		JSONArray object = JSONArray.fromObject(models);
		view.addObject("models", object);
		return view;
	}
	
	@RequestMapping(value="/findMovsMp4", method=RequestMethod.POST)
	public ModelAndView findMovsMp4(String movsPath){
		ModelAndView view = new ModelAndView("/json"); 	
		String mp4url = userService.findMovsMp4(movsPath);
		MovsModel model = new MovsModel();
		model.mp4_url = mp4url;
		JSONObject jObj = JSONObject.fromObject(model);
		view.addObject("models", jObj);
		return view;
	}
	
	/**
	 * 获取最新的数据
	 * @return
	 */
	@RequestMapping(value="/updateNewData", method=RequestMethod.GET)
	public void updateNewData(){
		userService.updateNewData();
	}
	
	/**
	 * 充值
	 */
	@RequestMapping(value="/recharge", method=RequestMethod.POST)
	public ModelAndView recharge(String days,String uid){
		ModelAndView view = new ModelAndView("/json"); 	
		JSONObject jObj = new JSONObject();
		UserModel uModel = userService.recharge(days,uid);
		if(uModel.getId() != 0){
			jObj.put(RESULT, OK);
			
			jObj.put("user", uModel);
		}else{
			jObj.put(RESULT, NO);
		}
		view.addObject("models", jObj);
		return view;
	}
	
}
